from allpairspy import AllPairs

from api_py.enums.cookie import Cookie
from api_py.generate_case.template import template_case
from api_py.utils.file_sys import write_json

timestamp = [
    {"value": "1669593600000", "description": "合法时间戳"},
    {"value": "1569593600000", "description": "合法时间戳"},
    {"value": "166959360000a", "description": "不合法时间戳"},
]

user = [
    {"value": [], "description": "空用户"},
    {"value": ["ff80818184cdbf5e0184d09835af00ea"], "description": "存在的用户"},
    {"value": ["ff80818184cdbf5e0184d09835af00ea", "ff80818184cdbf5e0184d09835afooad"], "description": "不存在的用户"},
]

type = [
    {"value": "COUNSELOR", "description": "咨询师排班"},
    {"value": "SUPERVISOR", "description": "督导排班"},
    {"value": "INVALID_USER", "description": "非法角色"},
]


cookies = [
    {"value": Cookie.ADMIN_COOKIE.value, "description": "管理员登录"},
    {"value": Cookie.SUPERVISOR_COOKIE.value, "description": "督导登录"},
    {"value": Cookie.COUNSELOR_COOKIE.value, "description": "咨询师登录"},
    {"value": Cookie.WEIXIN_COOKIE.value, "description": "微信访客登录"},
    {"value": Cookie.NULL_COOKIE.value, "description": "未登录"},
    {"value": Cookie.ERROR_COOKIE.value, "description": "其他Cookie"},
]

# 检测权限的用例
data = [
    {"value": "1669593600000", "description": "合法时间戳"},
    {"value": ["ff80818184524b500184c292a1ca0488"], "description": "存在的用户"},
    {"value": "COUNSELOR", "description": "咨询师排班"},
]


def case22_arrange(api_type):
    json_result = {}
    parameters = [timestamp, user, type]
    i = 0

    # 检测数据格式的用例
    for pairs in AllPairs(parameters):
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, pairs)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookies[0]

        json_result[i] = case
        i += 1

    # 检测用户权限用例
    for cookie in cookies:
        case = template_case()
        request = case["request"]
        # 设置url参数
        wrap_data(request, data)
        # 设置请求头cookie
        request["headers"]["Cookie"] = cookie

        json_result[i] = case
        i += 1

    write_json(api_type.value[0]["file_name"], json_result)

# 打包用例请求参数
def wrap_data(request, data):
    request["body"]["timestamp"] = data[0]
    request["body"]["users"] = data[1]
    request["body"]["type"] = data[2]